package com.jthinker.web.controller.admin;

import com.jthinker.bean.vo.QueryResult;
import com.jthinker.bean.vo.SearchFilter;
import com.jthinker.service.CategoryService;
import com.jthinker.web.controller.common.BaseController;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: Gang Chen
 * Date: 2015/4/22 21:55
 */
@Controller
@RequestMapping("/category")
public class CategoryController extends BaseController
{
    @Resource
    private CategoryService categoryService;

    @RequestMapping
    public String view(Model model)
    {
        List<Map<String, Object>> list = categoryService.findAll();
        model.addAttribute("list", list);
        return "category/categoryView";
    }

    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> menuList(HttpServletRequest request)
    {
        SearchFilter filter = getSearchFilter(request);
        QueryResult<Map<String, Object>> queryResult = categoryService.query(filter);
        return getSuccessMessage(queryResult);
    }

    @RequestMapping("/categoryAdd")
    public String categoryAdd(HttpServletRequest request)
    {
        String name = request.getParameter("name");
        String slug = request.getParameter("slug");
        String parentId = request.getParameter("parentId");

        Map<String, Object> record = new HashMap<String, Object>();
        record.put("name", name);
        record.put("slug", slug);
        record.put("parent_id", parentId);

        List<String> errors = new ArrayList<String>();
        if (StringUtils.isBlank(name))
        {
            errors.add("栏目名不能为空!");
        }

        if (StringUtils.isBlank(slug))
        {
            errors.add("别名不能为空!");
        }

        if (!categoryService.isSlugUnique(slug))
        {
            errors.add("栏目别名重复!");
        }

        if (CollectionUtils.isNotEmpty(errors))
        {
            request.setAttribute("errors", errors);
            return "category/categoryView";
        }

        try
        {
            categoryService.add(record);
        }
        catch (Exception e)
        {
            e.printStackTrace();
            errors.add("保存失败!");
        }

        return "category/categoryView";
    }

    @RequestMapping("/delete")
    @ResponseBody
    public Map<String, Object> delete(@RequestParam(value = "id", required = false) String id)
    {
        if (StringUtils.isEmpty(id))
        {
            return getFailMessage("请选择行!");
        }

        try
        {
            categoryService.delete(id);
        }
        catch (Exception e)
        {
            return getFailMessage("删除失败!");
        }
        return getSuccessMessage("删除成功!");
    }

}
